package mya.mes.mdc.model.snapshot;


import mya.mes.mdc.model.plc.TagPrefix;
import mya.mes.mdc.model.tag.*;
import mya.mes.mdc.model.tag.values.MdcTagValues;

import java.util.HashMap;


public class MdcSnapShot {

  private final static MdcSnapShot _instance = new MdcSnapShot();

  private static final String AI = TagPrefix.PREFIX_NUMBER;
  private static final String DI = TagPrefix.PREFIX_BOOLEAN;
  private static final String ST = TagPrefix.PREFIX_STRING;


  public static HashMap<String, Boolean> ALARM_MAP = new HashMap<>();

  /**
   * AI 数值型
   */
  private MdcTagValue[] AIArray = new MdcTagValue[MdcConstants.MDC_TAG_MAX];
  /**
   * DI 布尔量
   */
  private MdcTagValue[] DIArray = new MdcTagValue[MdcConstants.MDC_TAG_MAX];
  /**
   * 字符串
   */
  private MdcTagValue[] STArray = new MdcTagValue[MdcConstants.MDC_TAG_MAX];


  public static MdcSnapShot getInstance() {
    return _instance;
  }


  public void update(MdcTagValue tagValue) {
    if (TagPrefix.PREFIX_STRING.equals(tagValue.tagPrefix)) {
      STArray[tagValue.tagIndex] = tagValue;
      return;
    }

    if (TagPrefix.PREFIX_NUMBER.equals(tagValue.tagPrefix)) {
      AIArray[tagValue.tagIndex] = tagValue;
      return;
    }

    if (TagPrefix.PREFIX_BOOLEAN.equals(tagValue.tagPrefix)) {
      DIArray[tagValue.tagIndex] = tagValue;
      return;
    }
  }


  public void update(MdcTagValues tagValues) {

    if (tagValues.valueList == null) {
      return;
    }

    tagValues.valueList.forEach(tagValue -> {
      update(tagValue);
    });
  }


  public MdcTagValue getByMdcTag(MdcTag mdcTag) {
    int tagIndex = mdcTag.tagIndex;
    String tagPrefix = mdcTag.tagPrefix;

    if (tagIndex < 0) {
      throw new RuntimeException("tagIndex < 0: tagIndex = " + tagIndex);
    }

    if (tagIndex >= MdcConstants.MDC_TAG_MAX) {
      throw new RuntimeException("tagIndex > MAX_VALUE: tagIndex = " + tagIndex);
    }


    if (AI.equals(tagPrefix)) {
      return AIArray[tagIndex];
    }

    if (DI.equals(tagPrefix)) {
      return DIArray[tagIndex];
    }

    if (ST.equals(tagPrefix)) {
      return STArray[tagIndex];
    }


    throw new RuntimeException("Unknown TagPrefix:" + tagPrefix);
  }


  public String getDigitsOrPoste(String viewTagName, boolean digits) {

    ViewTag viewTag = ViewTagHolder.getInstance().getViewTag(viewTagName);

    if (viewTag == null) {
      return null;
    }

    MdcTag[] array = viewTag.digitsTagArray;

    if (digits) {
      array = viewTag.digitsTagArray;

    } else {
      array = viewTag.posteTagArray;

    }
    if (array == null) {
      return null;
    }

    MdcSnapShot mdcSnapShot = MdcSnapShot.getInstance();


    StringBuffer sb = new StringBuffer();

    for (int i = 0; i < array.length; i++) {
      if (array[i] == null) {
        sb.append("BB");
        continue;
      }

      MdcTagValue mdcTagValue = mdcSnapShot.getByMdcTag(array[i]);

      if (mdcTagValue == null) {
        sb.append("CC");
        continue;
      }

      if (mdcTagValue.parsedValue == null) {
        sb.append("DD");
        continue;
      }

      sb.append(mdcTagValue.parsedValue);
    }

    return sb.toString();
  }


  public String[] getPoste(String viewTagName) {

    ViewTag viewTag = ViewTagHolder.getInstance().getViewTag(viewTagName);

    if (viewTag == null) {
      return null;
    }

    MdcTag[] array = viewTag.posteTagArray;
    ;

    if (array == null) {
      return null;
    }

    MdcSnapShot mdcSnapShot = MdcSnapShot.getInstance();

    String[] posteArray = new String[array.length];

    // StringBuffer sb = new StringBuffer();

    for (int i = 0; i < array.length; i++) {
      if (array[i] == null) {
        //sb.append("BB");
        posteArray[i] = null;
        continue;
      }

      MdcTagValue mdcTagValue = mdcSnapShot.getByMdcTag(array[i]);

      if (mdcTagValue == null) {
        //   sb.append("CC");
        posteArray[i] = null;
        continue;
      }

      if (mdcTagValue.parsedValue == null) {
        posteArray[i] = null;
        continue;
      }
      posteArray[i] = mdcTagValue.parsedValue;
      ;
    }


    return posteArray;
  }
//  public String getString(TagValue tagValue) {
//
//  }
}


